package com.eagrelay;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import smsManagment.InfoSender;
import smsManagment.SmsFilter;
import smsManagment.SmsReceiver;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainMenu extends Activity {

	private SmsReceiver smsR;
	private String sms = "essai", resultat = "resultat";
	private TextView tv1, tv2;
	private Button bRun;
	private SmsReader sr;
	private EditText et;
	private Timer timer = new Timer();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);

		tv1 = (TextView) findViewById(R.id.textView2);
		tv2 = (TextView) findViewById(R.id.textView3);
		bRun = (Button) findViewById(R.id.button1);
		bRun.setTextColor(Color.GREEN);
		et = (EditText) findViewById(R.id.editText1);

		smsR = new SmsReceiver(getContentResolver());

		tv1.setText(sms);
		tv2.setText(resultat);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}

	public void run(View v) {
		bRun.setTextColor(Color.RED);
		Calendar now = Calendar.getInstance();
		Date nowDate = now.getTime();
		sr = new SmsReader(nowDate);
		timer.scheduleAtFixedRate(sr, 0, 2000);
		// on execute une tache toutes les 2 sec a partir de cette commande pour
		// la demo il faut le regler sur 1 sec pour l exploitation
	}

	public void updateText(View v) {
		// Methode activable par bouton utile uniquement pour la demo
		if (et.getText().toString().equals("") == false) {
			sms = et.getText().toString();
		}
		tv1.setText(sms);
		SmsFilter sf = new SmsFilter(sms);
		sf.classify();
		sf.filter();
		String info = sf.getInfo();
		resultat += " : " + info;
		tv2.setText(resultat);
		InfoSender is = new InfoSender(info);
		is.send();

		if (is.getRepo() != null) {
			resultat = is.getRepo();
		}

	}

	public void stop(View v) {
		bRun.setTextColor(Color.GREEN);
		timer.cancel();
	}

	// --------------------------------------//
	private class SmsReader extends TimerTask {

		private SmsReader(Date now2) {
			smsR.setNow(now2);
		}

		@Override
		public void run() {

			// Recevoir trier lire et envoyer les infos des differents sms
			sms = smsR.getSms();
			/*
			 * En commentaire pour la demo SmsFilter sf = new SmsFilter(sms);
			 * sf.classify(); sf.filter(); InfoSender is = new
			 * InfoSender(sf.getInfo());
			 */

		}

	}

}
